package com.panda.springcloud.controller;/**
 * @author 乐于吕
 * * @date 2024/10/23
 */

import com.panda.springcloud.clients.ClazzClient;
import com.panda.springcloud.entity.Clazz;
import com.panda.springcloud.entity.Group;
import com.panda.springcloud.service.GroupService;
import com.panda.springcloud.vo.GroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 乐于吕
 * @CreateTime: 2024-10-23
 * @Description:
 */

@RestController
@RequestMapping("/groups")
public class GroupController {

    @Autowired
    private ClazzClient clazzClient;

    @Autowired
    private GroupService groupService;

    @GetMapping
    public List<GroupVO> groupList() {

        List<Group> groups = groupService.list();
        List<GroupVO> collect = groups.stream().map(group -> {
            GroupVO groupVO = new GroupVO();
            BeanUtils.copyProperties(group, groupVO);

            Integer clazzid = group.getClazzid();
            Clazz clazz = clazzClient.getById(clazzid);
            groupVO.setClazz(clazz);
            return groupVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @GetMapping("{id}")
    public Group  getGroupById(@PathVariable("id") Integer id){

        return groupService.getById(id);

    }

    @PostMapping
    public Group addGroup(@RequestBody Group group) {

        groupService.save(group);
        return group;

    }


}
